package com.example.javatwo.controller.cg.djg;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.javatwo.controller.cg.dwh.PageBean;
import com.example.javatwo.pojo.cg.djg.Purchase;
import com.example.javatwo.service.cg.djg.PurchaseSerivce;

@Controller
@RequestMapping(value="/purchase/")
public class PurchaseController {
	
	
	@Autowired
	private PurchaseSerivce purchaseSerivce;
	
	/**
	 * 主页面跳转,不能在这里写代码
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/index")
	public String index() throws Exception{
		return "cg/djg/cg";
	}
	
	/**
	 * 交易历史查询,不能在这里写代码
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/index_jsjycx")
	public String index_jsjycx() throws Exception{
		return "cg/djg/lsjycx";
	}
	/**
	 * 分页查询
	 * @param current
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findPaging")
	@ResponseBody
	public PageBean findPaging(int current) throws Exception{
		int count =Integer.parseInt(purchaseSerivce.count());
		Object pageobj = this.purchaseSerivce.findPaging(current);
		PageBean bean = new PageBean(count,current, pageobj);
		return bean;
	}
	
	/**
	 * 新增和修改主表
	 * @param purchase
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveinset")
	@ResponseBody
	public Object saveinset(@RequestBody Purchase purchase) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		purchaseSerivce.save(purchase);
		map.put("ruselt", "保存成功！");
		return map;
	}
	
	/**
	 * 单据日期和单据号码的联动数据查询判断
	 * @param date
	 * @return
	 */
	@RequestMapping("/findMaxBillno")
	@ResponseBody
	public Object findMaxBillno(String date){
		Map<String,Object> map=new HashMap<String,Object>();
		Date d=null;
		if(date==null||date.equals(" ")){
			d=new Date();
			date=new SimpleDateFormat("yyyy-MM-dd").format(d);
		}
		map.put("date", date);
		String billno=purchaseSerivce.findMaxBillno(date);
		map.put("billno",billno);
		return map;
		
	}
	
	/**
	 * 删除主表（逻辑删除）
	 * @param billno
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delPurchase")
	@ResponseBody
	public Object delPurchase(String billno) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		purchaseSerivce.delPurchase(billno);
		map.put("ruselt", "删除成功！");
		return map;
	}
	
	/**
	 * 审核，反审核
	 * @param billno
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updatePurYnAudit")
	@ResponseBody
	public Object updatePurYnAudit(String billno,String yNAudit) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		purchaseSerivce.updatePurYnAudit(billno,yNAudit);
		map.put("ruselt", "成功！");
		return map;
	}
	

	/**
	 * 历史交易查询
	 * @param ftyid
	 * @return
	 */
	@RequestMapping("find_cg_jycx")
    @ResponseBody
	public Object find_cg_jycx(String ftyid) throws Exception{
		 return purchaseSerivce.find_cg_jycx(ftyid);
		
	}
	
}
